package 对称二叉树;

/**
 * @author: zhengtao
 * @description: 对称二叉树
 * @version: 1.0
 * @date: 2022/10/29  16:24
 */
public class Demo101 {

    /**
     * Definition for a binary tree node.
     * */
     public class TreeNode {
         int val;
         TreeNode left;
         TreeNode right;
         TreeNode() {}
         TreeNode(int val) { this.val = val; }
         TreeNode(int val, TreeNode left, TreeNode right) {
             this.val = val;
             this.left = left;
             this.right = right;
         }
     }

    public boolean isSymmetric(TreeNode root) {
        if (root == null) {
            return false;
        }
        return isDuicheng(root.left,root.right);

    }

    private boolean isDuicheng(TreeNode left, TreeNode right) {
         if(right==null&&left==null){
             return true;
         }else if(right!=null&&left!=null){
            if(right.val!=left.val) {
                return false;
            }
            return isDuicheng(right.left,left.right)&&isDuicheng(right.right,left.left);
         }else {
             return false;
         }
    }

}
